package com.wanlian.candy.controller;

import com.wanlian.candy.controller.session.UserSessionInfo;
import com.wanlian.common.ConstantCode;
import com.wanlian.common.PageCondition;
import com.wanlian.entity.Authority;
import com.wanlian.error.ErrorCode;
import com.wanlian.json.JsonResult;
import com.wanlian.json.JsonStringUtil;
import com.wanlian.model.ResultModel;
import com.wanlian.service.AuthorityServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/authority")
public class RoleAuthorityController {
    @Autowired
    private AuthorityServices authorityServices;
    /**
     * 添加权限
     * @param data
     * @return
     */
    @RequestMapping(value = "/add", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String add(@RequestBody String data, HttpSession session){
        UserSessionInfo sessionuser = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (sessionuser == null) {
            return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int userId = sessionuser.getLoginUser().getId();
        Authority authority = JsonStringUtil.readValue(data, Authority.class);
        authority.setCreateid(userId);
        ResultModel resultModel=authorityServices.doAdd(authority);
        return  new JsonResult().Serailze(resultModel);
    }

    /**
     * 修改权限
     * @param data
     * @return
     */
    @RequestMapping(value = "/edit", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String edit(@RequestBody String data, HttpSession session){
        UserSessionInfo sessionuser = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (sessionuser == null) {
            return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int userId = sessionuser.getLoginUser().getId();
        Authority authority = JsonStringUtil.readValue(data, Authority.class);
        authority.setCreateid(userId);
        ResultModel resultModel=authorityServices.doedit(authority);
        return  new JsonResult().Serailze(resultModel);
    }

    /**
     * 删除权限
     * @param data
     * @return
     */
    @RequestMapping(value = "/del", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String del(@RequestBody String data, HttpSession session){
        UserSessionInfo sessionuser = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (sessionuser == null) {
            return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int userId = sessionuser.getLoginUser().getId();
        Authority authority = JsonStringUtil.readValue(data, Authority.class);
        authority.setCreateid(userId);
        ResultModel resultModel=authorityServices.doDel(authority);
        return  new JsonResult().Serailze(resultModel);
    }

    /**
     * 权限查询
     * @param data
     * @return
     */
    @RequestMapping(value = "/query", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String query(@RequestBody String data, HttpSession session){
        UserSessionInfo sessionuser = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (sessionuser == null) {
            return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        ResultModel resultModel=authorityServices.queryPageList(pageCondition);
        return  new JsonResult().Serailze(resultModel);
    }

}
